package ezCall;

import net.rim.device.api.util.CharacterUtilities;


public class String_Cho  
{
                                              // ㄱ     ㄲ      ㄴ      ㄷ     ㄸ       ㄹ      ㅁ      ㅂ      ㅃ      ㅅ      ㅆ      ㅇ      ㅈ      ㅉ      ㅊ      ㅋ      ㅌ      ㅍ      ㅎ
	final static char[] ChoSung         = { 0x3131, 0x3131, 0x3134, 0x3137, 0x3137, 0x3139, 0x3141, 0x3142, 0x3142, 0x3145, 0x3145, 0x3147, 0x3148, 0x3148, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e };
	final static char[] ChoSung_Org     = { 0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139, 0x3141, 0x3142, 0x3143, 0x3145, 0x3146, 0x3147, 0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e };
	final static char[] ChoSung_Alpabet = {   'r' ,    'r',    's',    'e',    'e',    'f',    'a',    'q',   'q',    't' ,   't',    'd',     'w',   'w',     'c',   'z',   'x',    'v', 'g'     };
	final static int ChoSung_MaxSize = 19;
	                                          // ㅏ                ㅐ              ㅑ              ㅒ               ㅓ                 ㅔ                  ㅕ               ㅖ                ㅗ                 ㅘ                 ㅙ                ㅚ               ㅛ                ㅜ                   ㅝ                ㅞ               ㅟ                  ㅠ               ㅡ               ㅢ                  ㅣ
    final static char[] JwungSung           = { 0x314f, 0x3150, 0x3151, 0x3152, 0x3153, 0x3154, 0x3155, 0x3156, 0x3157, 0x3158, 0x3159, 0x315a, 0x315b, 0x315c, 0x315d, 0x315e, 0x315f, 0x3160, 0x3161, 0x3162, 0x3163 };
    final static char[] JwungSung_Alpabet = {    'k',    'o',    'i',   'o',    'j',    'p',    'u',    'u',    'h',    ' ',     ' ',   ' ',    'y',    'n',     ' ',   ' ',     ' ',   'b',    'm',     ' ',   'l' };
    final static int JwungSung_MaxSize = 21;
    
    
    //         ㄱ      ㄲ      ㄳ      ㄴ      ㄵ      ㄶ      ㄷ      ㄹ      ㄺ      ㄻ      ㄼ      ㄽ      ㄾ      ㄿ      ㅀ      ㅁ      ㅂ      ㅄ      ㅅ      ㅆ      ㅇ      ㅈ      ㅊ      ㅋ      ㅌ      ㅍ      ㅎ
    final static char[] JongSung  = { 0,      0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, 0x3139, 0x313a, 0x313b, 0x313c, 0x313d, 0x313e, 0x313f, 0x3140, 0x3141, 0x3142, 0x3144, 0x3145, 0x3146, 0x3147, 0x3148, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e };


                                                   // ㄱ     ㄲ      ㄴ      ㄷ     ㄸ       ㄹ      ㅁ      ㅂ      ㅃ      ㅅ      ㅆ      ㅇ      ㅈ      ㅉ      ㅊ      ㅋ      ㅌ      ㅍ      ㅎ
	final static char[] ChoSung_NoAlt_Perl     = { 0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139, 0x3141, 0x3142, 0x3143, 0x3145, 0x3146, 0x3147, 0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e };
    final static char[] ChoSung_Alt_Perl       = { 0x314b, 0x3131, 0x3134, 0x314c, 0x3137, 0x3139, 0x3141, 0x314d, 0x3142, 0x3145, 0x3145, 0x314e, 0x314a, 0x3148, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e };
                                                    // ㅋ                     ㅌ                              ㅍ                              ㅎ       ㅊ         

    final static char[] Alpabet_NoAlt_Perl     = { 'q', 'e', 't', 'u', 'o', 'a', 'd', 'g', 'j', 'z', 'c', 'b' };
    final static char[] Alpabet_Alt_Perl       = { 'w', 'r', 'y', 'i', 'p', 's', 'f', 'h', 'k', 'x', 'v', 'n' };
    
    final static int Alpabet_MaxSize = 12;
    
    public static String Get_ChoSung(String Str) 
    { 
        int a, b, c; // 자소 버퍼: 초성/중성/종성 순
        String result = "";

        for (int i = 0; i < Str.length(); i++) 
        {
        	char ch = Str.charAt(i);

        	// "AC00:가" ~ "D7A3:힣" 에 속한 글자면 분해
        	if (ch >= 0xAC00 && ch <= 0xD7A3) 
        	{ 

        		c = ch - 0xAC00;
        		a = c / (21 * 28);

        		result = result + ChoSung[a];
        	} else 
        	{
        		result = result + ch;
        	}
        }
        
        return result;
    }

    public static char Get_KoreanToEnglish( char ch )
    {
    	char Ret;

    	StringBuffer StrOutBuf = new StringBuffer();    
    	
    	StrOutBuf.append( ch );
    	
    	// 한글일경우
    	if (ch >= ChoSung_Org[0] && ch <= JwungSung[ JwungSung_MaxSize-1 ])
    	{ 
    		Ret = Get_ToggledStr( StrOutBuf.toString() ).charAt(0);
    	} else 
    	{
    		Ret = ch;
    	}
    	
    	return Ret;
    }
    
    public static char Get_EnglishToKorean( char ch )
    {
    	char Ret;

    	StringBuffer StrOutBuf = new StringBuffer();    
    	
    	StrOutBuf.append( ch );
    	
    	// 한글일경우
    	if (ch >= ChoSung_Org[0] && ch <= JwungSung[ JwungSung_MaxSize-1 ])
    	{ 
    		Ret = ch;
    	} else 
    	{
    		Ret = Get_ToggledStr( StrOutBuf.toString() ).charAt(0);
    	}
    	
    	StrOutBuf = null;
    	
    	return Ret;
    }
    
    public static String Get_ToggledStr(String Str) 
    { 
        int a, b, c; // 자소 버퍼: 초성/중성/종성 순
        String result = "";
        String StrSrc = Str.toLowerCase();
        
        for (int i = 0; i < StrSrc.length(); i++) 
        {
        	char ch = StrSrc.charAt(i);

        	
        	// "AC00:가" ~ "D7A3:힣" 에 속한 글자면 분해
        	if (ch >= ChoSung_Org[0] && ch <= JwungSung[ JwungSung_MaxSize-1 ]) 
        	{ 
        		// 한글일 경우
            	for( int j=0; j<ChoSung_MaxSize; j++ )
            	{
            		if( ChoSung_Org[j] == ch )
            		{
            			ch = ChoSung_Alpabet[j];
            			break;
            		}
            	}

            	for( int j=0; j<JwungSung_MaxSize; j++ )
            	{
            		if( JwungSung[j] == ch )
            		{
            			ch = JwungSung_Alpabet[j];
            			break;
            		}
            	}        	
            	
        		result = result + ch;
        	} else 
        	{
        		// 영문일 경우
            	for( int j=0; j<ChoSung_MaxSize; j++ )
            	{
            		if( ChoSung_Alpabet[j] == ch )
            		{
            			ch = ChoSung_Org[j];
            			break;
            		}
            	}

            	for( int j=0; j<JwungSung_MaxSize; j++ )
            	{
            		if( JwungSung_Alpabet[j] == ch )
            		{
            			ch = JwungSung[j];
            			break;
            		}
            	}
            	
            	
        		result = result + ch;
        	}
        }
        
        return result;
    }    

    public static char Get_KeyWithAlt( char ch ) 
    { 
    	char Ret;
    	
    	// 한글일경우
    	if (ch >= ChoSung_Org[0] && ch <= JwungSung[ JwungSung_MaxSize-1 ])
    	{ 
    		// 한글일 경우
        	for( int j=0; j<ChoSung_MaxSize; j++ )
        	{
        		if( ChoSung_NoAlt_Perl[j] == ch )
        		{
        			ch = ChoSung_Alt_Perl[j];
        			break;
        		}
        	}    		
    		Ret = ch;
    	} else 
    	{
        	for( int j=0; j<Alpabet_MaxSize; j++ )
        	{
        		if( Alpabet_NoAlt_Perl[j] == ch )
        		{
        			ch = Alpabet_Alt_Perl[j];
        			break;
        		}
        	}   
        	
        	Ret = ch;
    	}
    	
    	return Ret;
    }    
 
    public static char is_Char( char ch ) 
    { 
    	char Ret = 0;
    	
    	// 한글일경우
    	if (ch >= ChoSung_Org[0] && ch <= JwungSung[ JwungSung_MaxSize-1 ])
    	{ 
    		// 한글일 경우
        	for( int j=0; j<ChoSung_MaxSize; j++ )
        	{
        		if( ChoSung_NoAlt_Perl[j] == ch )
        		{
        			Ret = 1;
        			break;
        		}
        	}    		
    	} else 
    	{
        	for( int j=0; j<Alpabet_MaxSize; j++ )
        	{
        		if( Alpabet_NoAlt_Perl[j] == ch )
        		{
        			Ret = 1;
        			break;
        		}
        	}   
    	}
    	
    	return Ret;
    }       
}
